home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / uplabel.zip / UPLABEL.C < prev    next >
C/C++ Source or Header  |  1993-04-01  |  5KB  |  172 lines

  1. /*
  2.  * uplabel.c  John Dudeck  SIM International  19-Dec-90.
  3.  * simintl@rock.concert.net
  4.  * EasyLink 62013975+
  5.  *
  6.  * Released into the Public Domain.
  7.  *
  8.  * Text filter to generate multiple-up labels from 1-up label text.
  9.  *
  10.  * Command syntax:
  11.  * uplabel [-ln][-sn][-un][-wn] {infile | - } [outfile]
  12.  * -l number of lines per label, default -l6, max -l255
  13.  * -s number of spaces between labels, default -s1, max -s255
  14.  * -u number of columns up, default -u3, max -u255
  15.  * -w width of columns, default -w26, except -w32 for 4-up, max -w255
  16.  * if infile is '-', input is from stdin
  17.  * default outfile is stdout
  18.  *
  19.  * 'uplabel' alone on the command line causes a
  20.  * command summary to be printed to stdout.
  21.  *
  22.  * Processing of input text:
  23.  * The number of lines per label determines how many input lines
  24.  * are to be read in for each label.  Input lines longer than the column
  25.  * width will be truncated.  Input lines shorter than the column width
  26.  * will be padded with spaces.
  27.  *
  28.  * Lines are terminated by \n.  All other characters are passed through.
  29.  * This means that escape codes are passed unmodified.  However, this 
  30.  * may cause the current column to be incorrect, due to non-printing 
  31.  * characters.  
  32.  *
  33.  * Form feeds are passed through, and cause the
  34.  * counting of input lines-per-label to be reset.
  35.  */
  36.  
  37. #define YES 1
  38. #define NO 0
  39. #include <stdio.h>
  40. #ifdef BSD
  41. #include <strings.h>
  42. #else
  43. #include <string.h>
  44. #define    index    strchr
  45. #endif
  46.  
  47. void usage(void);
  48.  
  49. main(argc, argv)
  50. int argc;
  51. char *argv[];
  52. {
  53.    extern char *optarg;
  54.    char *buff;
  55.    int c, linlen, bufsiz;
  56.    int in_fnd = NO, out_fnd = NO;
  57.    int ln_per_lab = 6, cols_up = 3, col_wid = 26, spacs = 1;
  58.    int dflt_wid = YES, eoinput = NO;
  59.    FILE *in_fp, *out_fp = stdout;
  60.    
  61.    if (argc == 1) usage();
  62.    initarg(argc - 1, argv + 1);
  63.    while ((c = getarg("l:s:u:w:")) != 0) {
  64.       switch (c) {
  65.          case 'l':
  66.             ln_per_lab = atoi(optarg);
  67.             if (ln_per_lab < 1 || ln_per_lab > 255) usage();
  68.             break;
  69.          case 's':
  70.             spacs = atoi(optarg);
  71.             if (spacs < 0 || spacs > 255) usage();
  72.             break;
  73.          case 'u':
  74.             cols_up = atoi(optarg);
  75.             if (cols_up < 1 || cols_up > 255) usage();
  76.             break;
  77.          case 'w':
  78.             col_wid = atoi(optarg);
  79.             if (col_wid < 1 || col_wid > 255) usage();
  80.             dflt_wid = NO;
  81.             break;
  82.          case -1:
  83.             if (!in_fnd) {
  84.                if ((in_fp = fopen(optarg, "r")) == NULL) {
  85.                   fprintf(stderr, "Can't open input file %s.\n", optarg);
  86.                   exit(1);
  87.                }
  88.                in_fnd = YES;
  89.             } else if (!out_fnd) {
  90.                if ((out_fp = fopen(optarg, "w")) == NULL) {
  91.                   fprintf(stderr, "Can't open output file %s.\n", optarg);
  92.                   exit(1);
  93.                }
  94.                out_fnd = YES;
  95.             } else {
  96.                usage();
  97.             }
  98.             break;
  99.          case '-':
  100.             in_fp = stdin;
  101.             in_fnd = YES;
  102.             break;
  103.          case '?':
  104.             usage();
  105.       }
  106.    }
  107.    /* Check if default column width needs to be used. */
  108.    if (dflt_wid) col_wid = (cols_up == 4) ? 32 : 26;
  109.  
  110.    /* Allocate buffer. */
  111.    linlen = cols_up * col_wid + (cols_up - 1) * spacs + 2;
  112.    bufsiz = ln_per_lab * linlen + 1;
  113.    buff = (char *) malloc(bufsiz);
  114.    
  115.    while (!eoinput) {
  116.       int curcol, curlin, curlen, curcnt;
  117.       int ch;
  118.       char *cur;
  119.       
  120.       (void) memset(buff, ' ', bufsiz);
  121.       for (curcol = 0; curcol < cols_up && !eoinput; curcol++) {
  122.          for (curlin = 0; curlin < ln_per_lab && !eoinput; curlin++) {
  123.             cur = buff + (curlin * linlen) + (curcol * (col_wid + spacs));
  124.             curcnt = 0;
  125.             while ((ch = getc(in_fp)) != EOF) {
  126.                if (ch == '\n') break;
  127.                if (curcnt++ < col_wid) {
  128.                   *cur++ = ch;
  129.                }
  130.             }
  131.             if (ch == EOF) eoinput = YES;
  132.          }
  133.       }
  134.       for (curlin = 1; curlin <= ln_per_lab; curlin++) {
  135.          *(buff + curlin * linlen - 1) = '\n';
  136.       }
  137.       *(buff + bufsiz - 1) = '\0';
  138.       fputs(buff, out_fp);
  139.    }
  140. }
  141.  
  142.  
  143. /*
  144.  * void usage(void);
  145.  */
  146. void usage(void)
  147. {
  148.    fprintf(stderr, "\n\
  149. Text filter to generate multiple-up labels from 1-up label text.\n\
  150. \n\
  151. Command syntax:\n\
  152.  uplabel [-ln][-sn][-un][-wn] {infile | - } [outfile]\n\
  153.  -l number of lines per label, default -l6, max -l255\n\
  154.  -s number of spaces between labels, default -s1, max -s255\n\
  155.  -u number of columns up, default -u3, max -u255\n\
  156.  -w width of columns, default -w26, except -w32 for 4-up, max -w255\n\
  157.  if infile is '-', input is from stdin\n\
  158.  default outfile is stdout\n");
  159.  
  160.    fprintf(stderr, "\n\
  161.  The number of lines per label determines how many input lines\n\
  162.  are to be read in for each label.  Input lines longer than the column\n\
  163.  width will be truncated.\n\
  164. \n\
  165.  All characters in input are passed through to the output.\n\
  166.  Non-printable characters may cause unexpected results.\n\
  167. \n\
  168.  By John Dudeck, SIM International, 19-Dec-90.  Public Domain.\n\
  169. ");
  170.    exit(0);
  171. }
  172.